<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input v-model="form.account" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input v-model="form.mobile" class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input v-model="form.code" class="input" type="text" placeholder="请输入验证码" />
        <span class="code" @click="send">
          {{ countTime === 0 ? '发送验证码' : countTime + '秒后重试'}}
        </span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.password" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.rePassword" class="input" type="password" placeholder="请确认密码" />
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即提交</a>
  </div>
</template>

<script>
import { reactive } from 'vue'
import { useCountDown } from '@/compositions'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/user'
import { message } from '@/components/Message'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  props: {
    openId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    // 表单数据对象
    const form = reactive({
      account: null,
      mobile: null,
      code: null,
      password: null,
      rePassword: null
    })

    // 倒计时
    const { countTime, start } = useCountDown(60)
    const send = async () => {
      if (countTime.value === 0) {
        await reqUserQQPatchCode(form.mobile)
        message({ type: 'success', text: '发送成功' })
        start()
      }
    }

    // 完善信息
    const store = useStore()
    const router = useRouter()
    const submit = async () => {
      const res = await reqUserQQPatchLogin({
        unionId: props.openId,
        ...form
      })
      store.commit('user/setProfile', res.result)
      router.push('/')
      message({ type: 'success', text: 'QQ完善信息成功' })
    }

    return {
      form,
      countTime,
      send,
      submit
    }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>

